package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.param.QueryBalanceParam;
import com.caiyi.financial.nirvana.sms.util.SignUtil;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Mario on 2017/12/19 0019.
 * 余额相关Service
 */
@Service
public class SmsBalanceService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询账户余额
     *
     * @param queryBalanceParam 账户参数
     * @return 余额
     */
    public String queryBalance(QueryBalanceParam queryBalanceParam) {
        //获取用户信息
        String key = String.format(JedisKey.SMS_USER, queryBalanceParam.getAccount());
        String userJson = JedisService.get(key);
        YyAssert.isTrue(StringUtils.isNotBlank(userJson), ReturnCodeEnum.ACCOUNT_ERROR);
        //TODO 直接转成对象
        SmsUser user = JSON.parseObject(userJson, SmsUser.class);
        //签名验证
        YyAssert.isTrue(SignUtil.verifySign(queryBalanceParam, user.getPassword(), queryBalanceParam.getSign()), ReturnCodeEnum.REQUEST_SIGN_ERROR);
        //返回余额
        key = String.format(JedisKey.SMS_BALANCE, queryBalanceParam.getAccount());
        String smsNum = JedisService.get(key);
        return smsNum;
    }
}
